package com.cybertron.ironhide.search.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.cybertron.ironhide.search.common.Constants;
import com.cybertron.ironhide.search.domain.Article;
import com.cybertron.ironhide.search.domain.Category;
import com.cybertron.ironhide.search.domain.Product;
import com.cybertron.ironhide.search.service.ArticleService;
import com.cybertron.ironhide.search.service.ProductService;

@Controller
@RequestMapping("/gp")
public class GroupController {

	@Autowired(required = false)
	private ProductService productService;

	@Autowired(required = false)
	private ArticleService articleService;

	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public String execute(@PathVariable("id") String id, Model model) {
		Product product = productService.selectByPrimaryKeyWithGroup(id);
		if (product == null) {
			return "group";
		}
		List<Product> products = productService.selectByGroupId(id);
		List<Category> categories = productService.getSiteMap(product
				.getCategoryId());

		List<Article> topArticles = articleService.selectTop(
				Constants.TOP_ARTICLE_TYPE, Constants.TOP_ARTICLE_COUNT - 1);
		List<Article> botArticles = articleService.selectTop(
				Constants.BOT_ARTICLE_TYPE, Constants.BOT_ARTICLE_COUNT);

		model.addAttribute("products", products);
		model.addAttribute("product", product);
		model.addAttribute("categories", categories);
		model.addAttribute("topArticles", topArticles);
		model.addAttribute("botArticles", botArticles);
		return "group";
	}
}
